﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web;
using Project.Engine.Utilities;
using Project.Module.DAO.BL;
using System.Web.SessionState;

namespace Project.Module.Controller
{
    public class LoginAccountController
    {
    }

    public partial class HomeController
    {
        [AcceptVerbs(HttpVerbs.Post)]
        public JsonResult LoginAccount(string name, string pass)
        {
            // login
            bool check = (new CustomerBL()).LoginCustomer(name, pass);
            if (check == true)  {
                Login(name);
                // return
                return Json(new { result = 0 }, JsonRequestBehavior.AllowGet);                
            } else {
                // return
                return Json(new { result = 1 }, JsonRequestBehavior.AllowGet);  // sai user hoac pass
            }
        }

        public void Login(string name) {
            Session["username"] = name;
        }



        [AcceptVerbs(HttpVerbs.Post)]
        public JsonResult ResetPass(string mail, string name) {
            string checkMail = String.Empty;
            checkMail = (new CustomerBL()).GetEmailByUsername(name);
            if (checkMail.CompareTo(mail) == 0) {
                bool check = false;
                //update pass
                string newPass = (new CustomerBL()).UpdateNewPassword(name, mail);
                check = (new CustomerBL()).SendMail(mail, newPass);
                if (check == true)
                {
                    return Json(new { result = 0 }, JsonRequestBehavior.AllowGet);
                }
                return Json(new { result = 1 }, JsonRequestBehavior.AllowGet);  // email kô tồn tại
            }
            return Json(new { result = 1 }, JsonRequestBehavior.AllowGet);  // email kô tồn tại
        }
        public ActionResult Logout()
        {
            if (Session["username"] != null && ((string)(Session["username"])).Length > 0)
            {
                Session["username"] = String.Empty;
            }

            Session.Abandon();
            Session.Clear();

            return View("/Views/Home/Index.cshtml");
        }
    }
}
